import Qt 4.7

Rectangle {
    id: chatTab
    color: "lightGrey"
    anchors.top: parent.tabHeader.bottom;anchors.bottom:parent.bottom
    width: parent.width
    property alias chatHistory:historyEdit.text
    property string ip
    signal sendMessage(string ip,string sendText)

    Rectangle
    {
        id:chatContext_Rect
        width: chatTab.width
        anchors.top:chatTab.top
        anchors.bottom:sendContext_Rect.top;anchors.bottomMargin:5
        color:"white"
        Flickable
        {
             id: flick
             width:chatContext_Rect.width;height:chatContext_Rect.height
             contentWidth: historyEdit.paintedWidth
             contentHeight: historyEdit.paintedHeight
             clip: true

             function ensureVisible(r)
             {
                 if (contentX >= r.x)
                     contentX = r.x;
                 else if (contentX+width <= r.x+r.width)
                     contentX = r.x+r.width-width;
                 if (contentY >= r.y)
                     contentY = r.y;
                 else if (contentY+height <= r.y+r.height)
                     contentY = r.y+r.height-height;
             }

             TextEdit {
                 id: historyEdit
                 width: flick.width
                 height: flick.height
                 focus: true
                 wrapMode: TextEdit.Wrap
                 onCursorRectangleChanged: flick.ensureVisible(cursorRectangle)
             }
         }
    }
    Rectangle
    {
        id:sendContext_Rect
        width: chatTab.width; height: 40
        color:"white"
        anchors.bottom:sendButton.top;anchors.bottomMargin:5
        Flickable
        {
             id: flick2
             width:sendContext_Rect.width;height:sendContext_Rect.height
             contentWidth: sendText.paintedWidth
             contentHeight: sendText.paintedHeight
             clip: true

             function ensureVisible(r)
             {
                 if (contentX >= r.x)
                     contentX = r.x;
                 else if (contentX+width <= r.x+r.width)
                     contentX = r.x+r.width-width;
                 if (contentY >= r.y)
                     contentY = r.y;
                 else if (contentY+height <= r.y+r.height)
                     contentY = r.y+r.height-height;
             }

             TextEdit
             {
                 id: sendText
                 width: flick.width
                 height: flick.height
                 focus: true
                 wrapMode: TextEdit.Wrap
                 onCursorRectangleChanged: flick.ensureVisible(cursorRectangle)
             }
         }
    }

    TextButton
    {
        id:sendButton
        text: "send"
        anchors.bottom: chatTab.bottom;anchors.bottomMargin: 5
        anchors.right: sendContext_Rect.right;anchors.rightMargin: 5
        onClicked:{ console.log("in chattab");sendMessage(ip,sendText.text);sendText.text=""}
    }

    function messageUpdate(recieveText)
    {
        console.log("in chattab,udpate")
        historyEdit.text = recieveText
    }
}
